package com.mango.repository.convert;

import com.alibaba.fastjson.JSONObject;
import com.mango.facade.param.Category;
import com.mango.facade.param.TemplateAttribute;
import com.mango.repository.model.AmazonCategory;
import com.mango.repository.model.AmazonTemplateAttribute;

import java.util.Arrays;

/**
 * 转换
 */
public class Convert {

    public static Category toCategory(AmazonCategory model) {
        if (model == null) {
            return null;
        }
        Category category = new Category();
        category.setBrowseNodeId(model.getBrowseNodeId());
        category.setSite(model.getSite());

        category.setBrowseNodeAttributes(model.getBrowseNodeAttributes());
        category.setBrowseNodeName(model.getBrowseNodeName());
        category.setBrowseNodeStoreContextName(model.getBrowseNodeStoreContextName());
        category.setBrowsePathById(model.getBrowsePathById());
        category.setBrowsePathByName(model.getBrowsePathByName());
        if ("1".equals(model.getHasChildren())) {
            category.setChildNodes(Arrays.asList(model.getChildNodes().split(",")));
        }

        category.setHasChildren(model.getHasChildren());
        category.setProductTypeDefinitions(Arrays.asList(model.getProductTypeDefinitions().split(",")));
        category.setRefinementsInformation(model.getRefinementsInformation());


        return category;
    }

    public static TemplateAttribute toTemplateAttribute(AmazonTemplateAttribute model) {
        if (model == null) return null;
        TemplateAttribute templateAttribute = new TemplateAttribute();
        templateAttribute.setId(model.getId());
        templateAttribute.setAttributeName(model.getAttributeName());
        templateAttribute.setTemplateName(model.getTemplateName());
        templateAttribute.setTitle(model.getTitle());
        templateAttribute.setComment(model.getComment());
        templateAttribute.setDescription(model.getDescription());
        templateAttribute.setMinUniqueItems(model.getMinUniqueItems());
        templateAttribute.setMaxUniqueItems(model.getMaxUniqueItems());
        templateAttribute.setAttributeType(model.getAttributeType());
        templateAttribute.setGroupName(model.getGroupName());
        templateAttribute.setIsShow(model.getIsShow());
        templateAttribute.setExamples(JSONObject.parseArray(model.getExamples(), String.class));
        templateAttribute.setEnums(JSONObject.parseArray(model.getEnums(), String.class));
        templateAttribute.setEnumNames(JSONObject.parseArray(model.getEnumNames(), String.class));
        templateAttribute.setChildTemplateAttribute(JSONObject.parseArray(model.getChildTemplateAttribute(), TemplateAttribute.class));

        return templateAttribute;

    }
}
